// pages/collageWare/collageWare.js
var WxParse = require('../../wxParse/wxParse.js');
// pages/collageWare/collageWare.js

var total_micro_second,settime;
// 时间格式化输出，如3:25:19 86。每10ms都会调用一次
function dateformat(micro_second) {
  // 秒数
  var second = Math.floor(micro_second / 1000);
  // 小时位
  var hr = Math.floor(second / 3600);
  // 分钟位
  var min = Math.floor((second - hr * 3600) / 60);
  // 秒位
  var sec = (second - hr * 3600 - min * 60);// equal to => var sec = second % 60;
  // 毫秒位，保留2位
  var micro_sec = Math.floor((micro_second % 1000) / 10);
  return hr + ":" + min + ":" + sec;
};
/* 毫秒级倒计时 */
function countdown(that) {
  // 渲染倒计时时钟
  that.setData({
    clock: dateformat(total_micro_second)
  });

  if (total_micro_second <= 0) {
    that.setData({
      clock: "已经截止"
    });
    // timeout则跳出递归
    return;
  }
  // settime = setTimeout(function () {
  //   // 放在最后--
  //   console.log('----')
  //   total_micro_second -= 1000;
  //   countdown(that);
  // },1000)
}
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods_log:[],
    page_nav:[
      {
        name:'商品详情'
      },
      {
        name:'成交记录'
      }
    ],
    page_nav_index:0,
    movies: [
      { url: "../image/list_15_03.png" },
      { url: "../image/list_15_03.png" },
      { url: "../image/list_15_03.png" },
      { url: "../image/list_15_03.png" }
    ],
    clock: '',
    num: 1,
    minusStatus: 'disable',
    _num: 1,
    display_evaluate: "none",
    display_collageWare: "block",
    display_details: "none",
    display_confirm: "none",

    collect: "inline-block",
    collect_select: "none",
    mySale: "none",
    logo:false,          
    sale: [
      {
        src: "../image/salebg_03.png",
        money: 10,
        use_way: "满100元可使用",
        name: "优惠券",
        time: "2018.05.23-2018.05.24",
        click_get: "点击领取",
        isSelect: false,
      },
      {
        src: "../image/salebg_03.png",
        money: 10,
        use_way: "满100元可使用",
        name: "优惠券",
        time: "2018.05.23-2018.05.24",
        click_get: "点击领取",
        isSelect: false,
      }
    ],
    _number2:0,
    show_haibao:false,//是否显示海报生成框
    goods_sid:0,
    haibao_url:''
  },

  // 海报生成 start
  open_haibao(){
    console.log(this.data.goods_sid)
    console.log(this.data.content.goodsid)
    wx.showLoading({
      title: '生成海报中',
    })
    let that = this;
    let create_post = function(){
      return new Promise(function (resolve, reject) {
        wx.request({
          url: app.data.Url + '/goods/GetPoster',
          data: {
            goods_id: that.data.content.goodsid,
            path: '/pages/collageWare/collageWare',
            id: that.data.goods_sid
          },
          method: 'post',
          success: function (res) {
            resolve(res)
          },
          complete: function () {
            wx.hideLoading();
          }
        })
      })
    }
    

    create_post().then(res=>{
      that.setData({
        show_haibao: true,
        haibao_url: res.data.data.replace("http","https")
      })
      console.log(res.data)
    })
    
      
    
  },
  close_haibao(){
    this.setData({
      show_haibao: false
    })
  },
  // 海报保存至相册
  save_to_xc() {
    let that = this;
    console.log("save_to_xc");
    wx.showLoading({
      title: '请稍后',
      mask: true,

    })
    wx.getImageInfo({
      src: this.data.haibao_url,
      success(res) {
        console.log(res)
        wx.saveImageToPhotosAlbum({
          filePath: res.path,
          success: function () {
            that.setData({
              show_haibao: false
            })

          },
          complete: function () {
            wx.hideLoading({

            })
          }
        })
      }
    })
    return

    this.setData({
      show_haibao: false
    })
  },
  // 海报生成 end
  change_page_nav(e){
    console.log(e.target.dataset.index);
    this.setData({
      page_nav_index: e.target.dataset.index
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    this.setData({
      goods_sid: options.id
    })
    let that = this;
    that.setData({
      ontype: options.ontype ? options.ontype:false,
      shopsid: options.id,
      goodsid: options.goodsid
    })
    //console.log(that.data.shopsid,'options.ontype')
    if (options.ontype == '0'){
      console.log('1----------------')
      wx.pageScrollTo({
        scrollTop:500,
        duration: 0
      })
    }else{
      console.log('0--------------')
    } 
    //判断是否登录
    if (!wx.getStorageSync('userInfo')) {
      that.setData({
        logo: true
      })
    }
    //app.navTo('../confirm/confirm')
    wx.request({
      url: app.data.Url + 'spell/xcxdefaults',
      data: {
        sid: options.id,
        goodsid: options.goodsid
      },
      success: function (e) {
        //console.log(e.data.data,' e.data.data')
        that.setData({
          content: e.data.data,
          goods: e.data.data.attr,
          sid: options.id,
        })
        console.log(that.data.content.spell_list,'content')
        WxParse.wxParse("result", "html", e.data.data.content, that, 5)
        total_micro_second = e.data.data.createtime
        wx.request({
          url: app.data.Url + 'comment/commentLists',
          data: {
            goodsid: e.data.data.goodsid,
            type:2
          },
          method: 'get',
          success: function (res) {
            console.log('商品评论', res.data.data)
            if (res.data.data.length > 0) {
              that.setData({
                comment: res.data.data[0],
                comments: res.data.data
              })
            }else{
              that.setData({
                comment:[],
                comments: res.data.data
              })
            }
            console.log(that.data.comment,'commentcommentcomment')
          }
        })
      }
    })
    wx.request({
      //url: app.data.Url + 'coupon/usercoupon',
      url: 'https://zhapp.4hl.cn/app/Coupon/couponApp',
      data: {
        uid: wx.getStorageSync('userInfo').uid,
        bid: app.data.address.id,
        youtype: 1
      },
      method: 'get',
      success: function (res) {
        console.log('res', res)
        that.setData({
          sale: res.data
        })
      }
    })

    //成交记录
    wx.request({
      //url: app.data.Url + 'coupon/usercoupon',
      url: 'https://zhapp.4hl.cn/app/goods/DealRecord',
      data: {
        goodsid: options.id,
        bid: app.data.address.id,
      },
      method: 'post',
      success: function (res) {
        // console.log(res.data.data)
        that.setData({
          goods_log: res.data.data
        })
      }
    })
  
  },

  //返回首页
  return:function(){
    wx.reLaunch({
      url: '../index/index'
    })
  },
  sale: function () {
    this.setData({
      mySale: "block",
      // add_confirm: "block",
    })
  },
  get_sale: function (e) {
    let that = this;
    console.log(e.currentTarget.dataset.id)
    if (e.currentTarget.dataset.num != 0){
      wx.showToast({
        icon: 'none',
        title: '已领过优惠券',
      })
    }else{
    wx.request({
      //url: app.data.Url + 'coupon/usercoupon',
      url: 'https://zhapp.4hl.cn/app/Coupon/getcoupon',
      data: {
        uid: wx.getStorageSync('userInfo').uid,
        disid: e.currentTarget.dataset.id,
        youtype: 1
      },
      method: 'get',
      success: function (res) {
        wx.request({
          //url: app.data.Url + 'coupon/usercoupon',
          url: 'https://zhapp.4hl.cn/app/Coupon/couponApp',
          data: {
            uid: wx.getStorageSync('userInfo').uid,
            bid: app.data.address.id,
            youtype: 1
          },
          method: 'get',
          success: function (res) {
            console.log('res', res)
            that.setData({
              sale: res.data
            })
          }
        })
        console.log('res', res)
        // that.setData({
        //   sale: res.data
        // })
      }
    })
    }
    return false;
    console.log(e.target.dataset.index)
    let index = e.target.dataset.index;
    this.data.sale[index].isSelect = !this.data.sale[index].isSelect;

    // src = this.data.sale[index].src
    if (this.data.sale[index].isSelect) {
      // this.setData({
      this.data.sale[index].src = "../image/has_get_03.png"
      // })
    }
    this.setData({
      sale: this.data.sale,
    })
  },
  bindMinus: function () {
    var num = this.data.num;
    if (num > 1) {
      num--;
    }
    var minusStatus = num > 1 ? 'normal' : 'disable';
    this.setData({
      num: num,
      minusStatus: minusStatus
    })
  },
  /*点击加号*/
  bindPlus: function (e) {
    console.log(e)
    var num = this.data.num;
    num++;
    var minusStatus = num > 1 ? 'normal' : 'disable';
    this.setData({
      num: num,
      minusStatus: minusStatus
    })
  },
  /*输入框事件*/
  bindManual: function (e) {
    var num = e.detail.value;
    var minusStatus = num > 1 ? 'normal' : 'disable';
    this.setData({
      num: num,
      minusStatus: minusStatus
    })
  },
  focus_click: function (e) {
    console.log(app.data.address)
    this.setData({
      _num: e.target.dataset.num
    });
    if (e.target.dataset.num == 1) {
      this.setData({
        display_evaluate: "none",
        display_collageWare: "block",
        display_details: "none"
      })
    };
    if (e.target.dataset.num == 2) {
      this.setData({
        display_evaluate: "block",
        display_collageWare: "none",
        display_details: "none"
      })
    };
    if (e.target.dataset.num == 3) {
      this.setData({
        display_evaluate: "none",
        display_collageWare: "none",
        display_details: "block"
      })
    };
  },
  has_focus_click: function (e) {
    // console.log(e.target.dataset.number)
    var that = this;
    var type = that.data.goodstype;
    that.setData({
      _number: e.target.dataset.number
    })
    var data = this.data.goods;
    if (type == 1) {
      that.setData({
        shangpin: data,
        money: data[e.target.dataset.number].price,
        stock: data[e.target.dataset.number].stock,
        goodsname: data[e.target.dataset.number].rule,
        goodsid: data[e.target.dataset.number].goodsid,
        ruleid: data[e.target.dataset.number].attrid,
        goodstype: 1,
        _number: e.target.dataset.number,
      })
    } else {
      that.setData({
        shangpin: data,
        money: data[e.target.dataset.number].min_price,
        stock: data[e.target.dataset.number].stock,
        goodsname: data[e.target.dataset.number].rule,
        goodsid: data[e.target.dataset.number].goodsid,
        ruleid: data[e.target.dataset.number].attrid,
        goodstype: 2,
        _number: e.target.dataset.number,
      })
    }


  },
  // 增加数量
  addCount: function (e) {
    console.log(e)
    var num = this.data.num;
    num++;
    this.setData({
      num: num,
    })
  },
  // 减少数量
  minusCount: function (e) {
    var num = this.data.num;
    if (num > 1) {
      num--;
    }
    var minusStatus = num > 1 ? 'normal' : 'disable';
    this.setData({
      num: num,
      minusStatus: minusStatus
    })
  },
  buy_now: function (e) {
    let that = this;
    var id = e.currentTarget.dataset.id;

    this.setData({
      display_confirm: "block",
    })
    if (id == 1) {
      var data = this.data.goods;
      that.setData({
        shangpin: data,
        money: data[0].price,
        goodsid: data[0].goodsid,
        ruleid: data[0].attrid,
        stock: data[0].stock,
        goodsname: data[0].rule,
        goodstype: 1,
        _number: 0,
      })
    } else {
      var data = this.data.goods;
      that.setData({
        shangpin: data,
        goodsid: data[0].goodsid,
        ruleid: data[0].attrid,
        money: data[0].min_price,
        stock: data[0].stock,
        goodsname: data[0].rule,
        goodstype: 2,
        _number: 0,
      })
    }

  },
  hideview: function () {
    this.setData({
      display_confirm: "none",
      mySale: "none",
    })
  },
  collect: function () {
    this.setData({
      collect: "none",
      collect_select: "inline-block"
    })
  },
  collect_select: function () {
    this.setData({
      collect: "inline-block",
      collect_select: "none"
    })
  },
  onShareAppMessage: function (res) {
    console.log(this.data.shopsid + '------goodsid:' + this.data.goodsid)
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '快来和我拼团吧',
      path: '/pages/collageWare/collageWare?id='+this.data.shopsid+'&goodsid='+this.data.goodsid,
      success: function (res) {
        
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },


  //登录
  sendRequest: function (res) {
    let that = this;
    if (!res.detail.rawData) {
      wx.showModal({
        title: '提示',
        content: '请重新点击授权',
      })
      wx.hideLoding()
      return false;
    }
    wx.showLoading({
      title: '请稍后',
    })
    wx.login({
      success: function (code) {
        var code = code.code;
        wx.request({
          url: app.data.Url + 'User/logins',
          data: {
            utoken: '',
            code: code,
            encryptedData: res.detail.encryptedData,
            iv: res.detail.iv,
            bid: 123
          },
          method: 'post',
          success: function (rs) {
            if (rs.data.success == 10) {
              wx.request({
                url: app.data.Url + 'User/getUid',
                data: {
                  openid: rs.data.openid
                },
                method: 'post',
                success: function (e) {
                  // if (!e.data.data) wx.showModal({
                  //   title: '提示',
                  //   content: '请重新点击授权',
                  // })
                  // wx.hideLoading()
                  wx.setStorageSync('userInfo', {
                    head: res.detail.userInfo.avatarUrl,
                    name: res.detail.userInfo.nickName,
                    sex: res.detail.userInfo.gender,
                    uid: e.data.data,
                    openid: rs.data.openid
                  })
                  wx.request({
                    url: app.data.Url + 'user/get_shop',
                    data: {
                      uid: wx.getStorageSync('userInfo').uid
                    },
                    method: 'get',
                    success: function (res) {
                      if (res.data.data.id == '' || res.data.data.id == undefined || res.data.data.id == null) {
                        wx.getLocation({
                          type: 'wgs84',
                          success: function (res) {
                            var latitude = res.latitude
                            var longitude = res.longitude
                            wx.request({
                              url: app.data.Url + 'wisdom/get_add',
                              data: {
                                latitude: latitude,
                                longitude: longitude
                              },
                              method: 'post',
                              success: function (res) {
                                console.log(res.data.data.bid, 'res.data.data')
                                app.data.address.name = res.data.data.name
                                app.data.address.id = res.data.data.bid
                                that.setData({
                                  address: res.data.data.name,
                                  bid: res.data.data.bid
                                })
                              }
                            })
                          }
                        })
                        console.log('true')
                      } else {
                        //绑定社区
                        app.data.address.name = res.data.data.name
                        app.data.address.id = res.data.data.id
                        console.log(app.data.address, '=============')
                        that.setData({
                          address: res.data.data.name,
                          bid: res.data.data.id
                        })
                      }
                      that.setData({
                        logo: false
                      })
                      wx.hideLoading()
                    }
                  })

                  //请求绑定社区
                  wx.setStorageSync('phone', e.data.phone)

                  // wx.showTabBar({
                  //   success: function () {
                  //     that.setData({
                  //       logo: false
                  //     })
                  //     wx.showTabBar({

                  //     })
                  //     wx.hideLoading()
                  //   }
                  // })

                }
              })
            } else {
              wx.showModal({
                title: '提示',
                content: '请重新点击授权',
              })
              wx.hideLoading()
              return false;
            }
          }
        })
      }
    })
  },

  click:function(){
    let that = this;
    var goodstype = that.data.goodstype;
    console.log(goodstype,'goodstype')
    //../confirmd/confirm ? goodsid = {{ goodsid }} & type={{ goodstype }}& rule={{ _number }}& num={{ num }}& sid={{ sid }}
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that =this;
    //var user = wx.getStorageSync('userInfo').uid;
    // if (!user){
    //   that.setData({
    //     logo:true
    //   })
    // }else{
    //   that.setData({
    //     logo: false
    //   })
    //   wx.showToast({
    //     title: '已登录',
    //   })
    // }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (this.data.ontype){
      wx.switchTab({
        url: '../index/index',
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})